define(function(require) {
	require("cordova!com.justep.cordova.plugin.barcodescanner");
	var Model = function() {
		this.callParent();
	};

	Model.prototype.modelLoad = function(event) {
		console.log("Main.js modelLoad");
		// debugger;
		var api_url = window.app.base_url + window.app.ProductApi;
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				window.app.ProductApi_ret = res;
				return res;
			},
			error : function(e) {
				return {};
			}
		});
		this.comp("ScanningDelivery_Window").load("$UI/ls_shaomiao/DistributorSelection.w");
	};
	Model.prototype.ScanningDelivery_ButtonClick = function(event) {
		this.comp("ScanningDelivery_Window").load("$UI/ls_shaomiao/DistributorSelection.w");
	};
	Model.prototype.ShippingRecord_ButtonClick = function(event) {
		this.comp("ShippingRecord_Window").load("$UI/ls_shaomiao/Log.w");
	};
	Model.prototype.DistributorManagement_ButtonClick = function(event) {
		this.comp("DistributorManagement_Window").load("$UI/ls_shaomiao/Subagent.w");
	};
	Model.prototype.PersonalCenter_ButtonClick = function(event) {
		this.comp("PersonalCenter_Window").load("$UI/ls_shaomiao/Person.w");
	};
	Model.prototype.Main_ContentsActiveChanged = function(event) {
		console.log("Main_ContentsActiveChanged()");
		console.log(event);
		if (event.from === 5 && event.to === 4) {
			console.log("扫码页面返回到添加产品页面");
			var mod = this.comp("AddProducts_Window").getInnerModel();
			var scanFinish = true;
			mod.comp("FaHuoLieBiaoData").each(function(param) {
				if (param.row.val('ScanCount') !== param.row.val('PrdCount'))
					scanFinish = false;
			});
			if (scanFinish === true) {
				$(mod.getElementByXid("SendButton")).show();
			} else {
				$(mod.getElementByXid("SendButton")).hide();
			}

		}
		if (event.from === 9 && event.to === 8) {
			console.log("扫码页面返回到添加产品页面");
			var mod2 = this.comp("AddProductsSell_Window").getInnerModel();
			var scanFinish2 = true;
			mod2.comp("FaHuoLieBiaoData").each(function(param) {
				if (param.row.val('ScanCount') !== param.row.val('PrdCount'))
					scanFinish2 = false;
			});
			if (scanFinish2 === true) {
				$(mod2.getElementByXid("SendButton")).show();
			} else {
				$(mod2.getElementByXid("SendButton")).hide();
			}

		}
	};
	return Model;
});
